package cac.fenjian.resource.partSalesReturn.service;


import cac.common.DateUtil;
import cac.fenjian.resource.WmsDeliverPlan.dto.WmsDeliverPlanDTO;
import cac.fenjian.resource.WmsReturnApply.dto.WmsReturnApplyDTO;
import cac.fenjian.resource.partSalesReturn.dao.DeliverPlanDao;
import cac.fenjian.resource.partSalesReturn.dao.PartSalesReturnDao;
import com.cac.dme.adapter.rest.msg.QueryReqBean;
import com.cac.dme.adapter.rest.msg.QueryRespBean;
import com.cac.dme.adapter.utils.ComUtil;
import com.cac.dme.adapter.utils.PageHelper;
import com.cac.dme.adapter.utils.PojoUtil;
import com.cac.dme.common.exception.service.DaoException;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PartSalesReturnServiceImpl implements PartSalesReturnService{
    @Autowired
    private PartSalesReturnDao partSalesReturnDao;
    @Autowired
    private DeliverPlanDao deliverPlanDao;
    @Transactional
    @Override
    //插入退货单
    public int insertWmsReturnApply(Map map) {
        try{
            //判断退库数量是否大于库存数量
            Integer returnQty = Integer.valueOf(String.valueOf(map.get("returnQty")));
            String wmsDeliveryPlanId = String.valueOf(map.get("wmsDeliveryPlanId"));
            HashMap<String,String> dto = partSalesReturnDao.selectItemQty(wmsDeliveryPlanId);
            Integer currentOnhandQty = Integer.valueOf(String.valueOf(dto.get("currentOnhandQty")));
            Integer engagedQty = Integer.valueOf(String.valueOf(dto.get("engagedQty")));
            //当前库存减非离库占用
            int realityQty = currentOnhandQty - engagedQty;
            //判断退库数量是否大于库存数量
            if (returnQty>currentOnhandQty){
                int msg =-2;
                return msg;
             //判断退库数量是否大于（库存数量-非离库占用数量）
            }else if (returnQty>realityQty){
                int msg =-3;
                return msg;
            }else {
                WmsReturnApplyDTO wmsReturnApplyDTO = new WmsReturnApplyDTO();
                wmsReturnApplyDTO.setWmsReturnApplyId(ComUtil.getId());
                String status = "1";
                wmsReturnApplyDTO.setReturnStatus(status);
                wmsReturnApplyDTO.setApplyDate(DateUtil.nowTime());
                return partSalesReturnDao.insertWmsReturnApply(wmsReturnApplyDTO,map);
            }

        }catch (Exception e){
            throw new DaoException(e.getMessage());
        }
    }
    @Transactional
    @Override
    //零件配送页面条件查询
    public QueryRespBean<WmsDeliverPlanDTO> selectDeliverPlan(QueryReqBean<WmsDeliverPlanDTO> queryReqBean, String orderBy, String Keywords) {
        QueryRespBean<WmsDeliverPlanDTO> queryRespBean =new QueryRespBean<>();
        try {
            PageHelper.startPage(queryReqBean.getPageParameter());
            WmsDeliverPlanDTO searchParams = queryReqBean.getSearchParams();
            Page<WmsDeliverPlanDTO> dataList = deliverPlanDao.selectDeliverPlan(searchParams,orderBy,Keywords);
            queryRespBean.setResult(dataList);
            return queryRespBean;
        }catch (Exception e) {
            throw new DaoException(e.getMessage(), e);
        }
    }
    @Transactional
    @Override
    //成品配送页面条件查询
    public QueryRespBean<WmsDeliverPlanDTO> selectDeliverPlanProduct(QueryReqBean<WmsDeliverPlanDTO> queryReqBean, String orderBy, String Keywords) {
        QueryRespBean<WmsDeliverPlanDTO> queryRespBean =new QueryRespBean<>();
        try {
            PageHelper.startPage(queryReqBean.getPageParameter());
            WmsDeliverPlanDTO searchParams = queryReqBean.getSearchParams();
            Page<WmsDeliverPlanDTO> dataList = deliverPlanDao.selectDeliverPlanProduct(searchParams,orderBy,Keywords);
            queryRespBean.setResult(dataList);
            return queryRespBean;
        }catch (Exception e) {
            throw new DaoException(e.getMessage(), e);
        }
    }
    @Transactional
    @Override
    //零件退货页面条件查询
    public QueryRespBean<WmsReturnApplyDTO> selectReturnApply(QueryReqBean<WmsReturnApplyDTO> queryReqBean, String orderBy, String Keywords, Date startTime, Date endTime
    ) {
        QueryRespBean<WmsReturnApplyDTO> queryRespBean =new QueryRespBean<>();
        try {
            PageHelper.startPage(queryReqBean.getPageParameter());
            WmsReturnApplyDTO searchParams = queryReqBean.getSearchParams();
            Page<WmsReturnApplyDTO> dataList = partSalesReturnDao.selectReturnApply(searchParams,orderBy,Keywords,startTime,endTime);
            queryRespBean.setResult(dataList);
            return queryRespBean;
        }catch (Exception e) {
            throw new DaoException(e.getMessage(), e);
        }
    }
    @Transactional
    @Override
    //成品退货页面条件查询
    public QueryRespBean<WmsReturnApplyDTO> selectReturnApplyProduct(QueryReqBean<WmsReturnApplyDTO> queryReqBean, String orderBy, String Keywords, Date startTime, Date endTime
    ) {
        QueryRespBean<WmsReturnApplyDTO> queryRespBean =new QueryRespBean<>();
        try {
            PageHelper.startPage(queryReqBean.getPageParameter());
            WmsReturnApplyDTO searchParams = queryReqBean.getSearchParams();
            Page<WmsReturnApplyDTO> dataList = partSalesReturnDao.selectReturnApplyProduct(searchParams,orderBy,Keywords,startTime,endTime);
            queryRespBean.setResult(dataList);
            return queryRespBean;
        }catch (Exception e) {
            throw new DaoException(e.getMessage(), e);
        }
    }
    @Transactional
    @Override
    //修改退货单信息
    public int updateWmsReturnApply(WmsReturnApplyDTO wmsReturnApplyDTO) {
        try{
            Integer count = partSalesReturnDao.updateReturnApply(getUpdateDto(wmsReturnApplyDTO));
            if(count== null){
                throw new DaoException("修改数据失败");
            }
            return count;
        }catch (Exception e){
            throw new DaoException(e.getMessage(),e);
        }
    }
    @Transactional
    @Override
    //更新数据
    public WmsReturnApplyDTO getUpdateDto(WmsReturnApplyDTO wmsReturnApplyDTO) {
        WmsReturnApplyDTO oldDTO = partSalesReturnDao.findWmsReturnApplyById(wmsReturnApplyDTO.getWmsReturnApplyId());
        if(oldDTO == null){
            throw new DaoException("数据不存在");
        }
        PojoUtil.copyProperties(oldDTO,wmsReturnApplyDTO,true);
        return oldDTO;
    }
    @Transactional
    @Override
    //单条记录删除
    public int deleteWmsReturnApplyById(String wmsReturnApplyId){
        if (StringUtils.isEmpty(wmsReturnApplyId)) {
            throw new DaoException("删除失败！传入的参数主键为null");
        }
        try {
            //记录日志
            WmsReturnApplyDTO wmsReturnApplyDTO = partSalesReturnDao.findWmsReturnApplyById(wmsReturnApplyId);
            if (wmsReturnApplyDTO == null) {
                throw new DaoException("删除失败！对象不存在");
            }
            //删除业务数据
            int count = partSalesReturnDao.deleteWmsReturnApplyById(wmsReturnApplyId);
            return count;
        } catch (Exception e) {
            throw new DaoException(e.getMessage(), e);
        }
    }
    @Transactional
    @Override
    //批量删除
    public int deleteWmsReturnApplyByIds(HashMap<String,Object> map) {
        int count = partSalesReturnDao.deleteWmsReturnApplyById((String) map.get("wmsReturnApplyId"));
        return count;
    }
    @Transactional
    @Override
    //提交退货单信息
    public int changeReturnType(String wmsReturnApplyId) {
        try{
            WmsReturnApplyDTO wmsReturnApplyDTO = new WmsReturnApplyDTO();
            wmsReturnApplyDTO.setWmsReturnApplyId(wmsReturnApplyId);
            wmsReturnApplyDTO.setReturnStatus("4");
            Integer count = partSalesReturnDao.updateReturnApply(getUpdateDto(wmsReturnApplyDTO));
            if(count== null){
                throw new DaoException("提交数据失败！");
            }
            return count;
        }catch (Exception e){
            throw new DaoException(e.getMessage(),e);
        }
    }
    @Transactional
    @Override
    public int changeReturnTypes(List<String> idlist){
        int count = 0;
        for (String wmsReturnApplyId : idlist) {
            changeReturnType(wmsReturnApplyId);
            count++;
        }
        return count;
    }

    public List<HashMap> searchWorkbinMaterial(HashMap map){
        return partSalesReturnDao.searchWorkbinMaterial(map);
    }
}

